package intranet.client;

/**
 * @author Baptiste RENDU
 */

import java.util.ArrayList;
import java.util.HashMap;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class FileTree extends ScrollPanel{
	
	private final DataBaseServiceAsync dataBaseService = GWT
	.create(DataBaseService.class);
	protected Tree tree;
	protected ViewerTab viewer;

	protected ArrayList<String[]> courseMap,subjectMap,documentMap,courseSubjectMap;
	
	/**
	 * constructeur qui permet de créer un arbre de documents sur le serveur
	 * @param l'identifiant et le panel d'affichage de documents
	 */
	public FileTree(String login, ViewerTab v)
	{
		viewer = v;
		String reqCourse ="select course_id,course_name from course";
		dataBaseService.select(reqCourse, new AsyncCallback<HashMap<String,String[]>>() {
			

			@Override
			public void onSuccess(HashMap<String, String[]> result) {
				// TODO Auto-generated method stub
				courseMap=new ArrayList<String[]>();
				for(int i=0;i<result.get("course_id").length;i++)
				{
					String[] s={result.get("course_id")[i], result.get("course_name")[i]};
					courseMap.add(s);
				}
				String reqSubject ="select subject_id,subject_name from subject";
				dataBaseService.select(reqSubject, new AsyncCallback<HashMap<String,String[]>>() {
					
					@Override
					public void onSuccess(HashMap<String, String[]> result) {
						// TODO Auto-generated method stub
						subjectMap=new ArrayList<String[]>();
						for(int i=0;i<result.get("subject_id").length;i++)
						{
							String[] s={result.get("subject_id")[i], result.get("subject_name")[i]};
							subjectMap.add(s);
						}
						String reqCourseSubject ="select * from subject_course";
						dataBaseService.select(reqCourseSubject, new AsyncCallback<HashMap<String,String[]>>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								
							}

							@Override
							public void onSuccess(
									HashMap<String, String[]> result) {
								// TODO Auto-generated method stub
								courseSubjectMap=new ArrayList<String[]>();
								for(int i=0;i<result.get("course_id").length;i++)
								{
									String[] s={result.get("course_id")[i], result.get("subject_id")[i]};
									courseSubjectMap.add(s);
								}
								buildTree();
							}
						});
						
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert("erreur bado");
					}
				});
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("erreur bado");
			}
		});
		
		
		
	}
	/**
	 * methode de construction de l'arbre a partir des données de la base
	 */
	public void buildTree()
	{
		tree=new Tree();
		tree.addItem("/");

		for(int i=0;i<courseMap.size();i++)
		{
			TreeItem tempItem=tree.getItem(0).addItem(courseMap.get(i)[1]);
			for(int j=0;j<courseSubjectMap.size();j++)
			{
				if(courseMap.get(i)[0].equals(courseSubjectMap.get(j)[0]))
				{
					for(int k=0;k<subjectMap.size();k++)
					{
						if(subjectMap.get(k)[0].equals(courseSubjectMap.get(j)[1]))
						{
							Tree tempTree = new Tree();
							tempTree.addItem(subjectMap.get(k)[1]);
							tempTree.getItem(0).addItem("");
							tempItem.addItem(tempTree);
							tempTree.addOpenHandler(new OpenHandler<TreeItem>() {
								
								@Override
								public void onOpen(OpenEvent<TreeItem> event) {
									
									// TODO Auto-generated method stub
									final TreeItem item=event.getTarget();
									item.removeItems();
									String reqDocument ="select * from document where document_subject in (" +
											"select subject_id from subject where subject_name='"+item.getText()+"')";
									dataBaseService.select(reqDocument, new AsyncCallback<HashMap<String,String[]>>() {
		
										@Override
										public void onFailure(Throwable caught) {
											
										}
		
										@Override
										public void onSuccess(HashMap<String, String[]> result) {
											
											Tree tempTree=new Tree();
											tempTree.addSelectionHandler(new SelectionHandler<TreeItem>() {
												
												@Override
												public void onSelection(SelectionEvent<TreeItem> event) {
													// TODO Ouvrir document
													viewer.addDocument((String)event.getSelectedItem().getUserObject(), event.getSelectedItem().getText());
												}
											});
											for(int k=0;k<result.get("document_id").length;k++)
											{
												
												TreeItem t =tempTree.addItem(result.get("document_name")[k]);
												t.setUserObject(result.get("document_path")[k]);
											}
											if(tempTree.getItemCount()!=0)
												item.addItem(tempTree);
										}
							});
						}
					});
					break;
					}
					}
					
				}
			}
		}
		add(tree);
	}	
}
